package constant.milk.cashbook.asset.real;

import java.util.ArrayList;
import java.util.HashMap;

import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.asset.AddFlagActivity;
import constant.milk.cashbook.asset.CountActivity;
import constant.milk.cashbook.asset.MemoActivity;
import constant.milk.cashbook.asset.MoneyActivity;
import constant.milk.cashbook.asset.NameActivity;
import constant.milk.cashbook.bean.AutoMainData;
import constant.milk.cashbook.bean.HalbuData;
import constant.milk.cashbook.bean.InOutData;
import constant.milk.cashbook.bean.MainSettingData;
import constant.milk.cashbook.bean.RealData;
import constant.milk.cashbook.bean.SmsData;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.database.DbDeleteQuery;
import constant.milk.cashbook.database.DbUpdateQuery;
import constant.milk.cashbook.tip.TipMap;
import constant.milk.cashbook.tip.TipInfoActivity;
import constant.milk.cashbook.util.MilkManager;
import constant.milk.cashbook.util.MilkUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class RealUpdateActivity extends SuperActivity
{
	private static final int NAME_REQUEST_CODE = 0;
	private static final int MONEY_REQUEST_CODE = 1;
	private static final int COUNT_REQUEST_CODE = 2;
	private static final int MEMO_REQUEST_CODE = 3;
	
	private View nameView;
	private View moneyView;
	private View countView;
	private View memoView;
	
	private TextView nameTextView;
	private TextView moneyTextView;
	private TextView countTextView;
	private TextView memoTextView;
	
	private Button deleteButton;
	private Button updateButton;
	
	private RealData data;
	
	private DBSelectQuery select;
	
	private MilkManager milkManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.realmoney_update_activity);
		
		setTitle("현금정보");
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		nameView = findViewById(R.id.realUpdateNameView);
		moneyView = findViewById(R.id.realUpdateMoneyView);
		countView = findViewById(R.id.realUpdateCountView);
		memoView = findViewById(R.id.realUpdateMemoView);
		
		nameTextView = (TextView) findViewById(R.id.realUpdateNameTextView);
		moneyTextView = (TextView) findViewById(R.id.realUpdateMoneyTextView);
		countTextView = (TextView) findViewById(R.id.realUpdateCountTextView);
		memoTextView = (TextView) findViewById(R.id.realUpdateMemoTextView);
		
		deleteButton = (Button) findViewById(R.id.deleteButton);
		updateButton = (Button) findViewById(R.id.updateButton);
	}

	@Override
	protected void setOnClick()
	{
		nameView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, NameActivity.class);
				intent.putExtra(NameActivity.NAME_DATA, nameTextView.getText().toString());
				startActivityForResult(intent, NAME_REQUEST_CODE);
			}
		});
		
		moneyView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MoneyActivity.class);
				intent.putExtra(MoneyActivity.MONEY_DATA, moneyTextView.getText().toString());
				startActivityForResult(intent, MONEY_REQUEST_CODE);
			}
		});
		
		countView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, CountActivity.class);
				intent.putExtra(CountActivity.COUNT_DATA, countTextView.getText().toString());
				startActivityForResult(intent, COUNT_REQUEST_CODE);
			}
		});
		
		memoView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, MemoActivity.class);
				intent.putExtra(MemoActivity.MEMO_DATA, memoTextView.getText().toString());
				startActivityForResult(intent, MEMO_REQUEST_CODE);
			}
		});
		
		deleteButton.setOnClickListener(new OnClickListener()
		{
			
			public void onClick(View v)
			{
				if (setDeleteData())
				{
					MilkUtil.showDialogFinish(superContext, "확인", 
							"삭제에 성공하였습니다.");
				}
				else
				{
					MilkUtil.showDialog(superContext, "실패", "잠시후 다시해 주세요.");
				}
			}
		});
		
		updateButton.setOnClickListener(new OnClickListener()
		{
			
			public void onClick(View v)
			{
				if (setCheckData())
				{
					return;
				}
				
				if (setUpdateData())
				{
					MilkUtil.showDialogFinish(superContext, "확인", 
							"수정에 성공하였습니다.");
				}
				else
				{
					MilkUtil.showDialog(superContext, "실패", "잠시후 다시해 주세요.");
				}
			}
		});
	}

	@Override
	protected void setData()
	{
		select = new DBSelectQuery(superContext, superDb);
		
		milkManager = new MilkManager(superContext, superDb);
		
		Intent intent = getIntent();
		String id = intent.getStringExtra(RealActivity.REAL_ID);
		
		DBSelectQuery db = new DBSelectQuery(superContext, superDb);
		
		data = db.getRealId(id);
		
		nameTextView.setText(data.getName());
		
		String money = data.getMoney();
		money = MilkUtil.makeStringWithComma(money);
		moneyTextView.setText(money);
		
		countTextView.setText(data.getCount());
		memoTextView.setText(data.getMemo());
	}
	
	private boolean setUpdateData()
	{
		DbUpdateQuery query = null;
		
		try
		{
			query = new DbUpdateQuery(superContext, superDb);
			
			query.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("name", nameTextView.getText().toString());
			map.put("money", MilkUtil.getCommaDel(moneyTextView.getText().toString()));
			
			map.put("count", countTextView.getText().toString());
			
			map.put("memo", memoTextView.getText().toString());
			
			query.setRealMoneyUpdate(map, data.getId());
			
			milkManager.setUpdateAssetRollBank("0", data.getName(), nameTextView.getText().toString());
			
			ArrayList<InOutData> inOutList = select.getInOutRollBackList(0, data.getName());
			
			map.clear();
			map.put("name", nameTextView.getText().toString());
			
			for (int i = 0; i < inOutList.size(); i++)
			{
				query.setInOutUpdate(map, inOutList.get(i).getId());
			}
			
			ArrayList<InOutData> inOutSubList = select.getInOutRollBackList(0, data.getName());
			
			map.clear();
			map.put("subasset", nameTextView.getText().toString());
			
			for (int i = 0; i < inOutSubList.size(); i++)
			{
				query.setInOutUpdate(map, inOutSubList.get(i).getId());
			}
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}
	
	private boolean setDeleteData()
	{
		DbDeleteQuery query = null;
		try
		{
			query = new DbDeleteQuery(superContext, superDb);
			
			query.setBeginTansaction();
			
			query.setRealMoneyDelete(data.getId());
			
			milkManager.setDeleteAssetRollBank("0", data.getName());
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
			return false;
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * 테이터가 제대로 있는지 확인한다.
	 */
	private boolean setCheckData()
	{
		String name = nameTextView.getText().toString();
		
		if (name.equals("") || name.length() < 1)
		{
			MilkUtil.showDialog(superContext, "확인", "이름을 입력해주세요.");
			return true;
		}
		
		DBSelectQuery query = new DBSelectQuery(superContext, superDb);
		if (!data.getName().equals(name) && query.getRealCount(name) > 0)
		{
			MilkUtil.showDialog(superContext, "확인", "같은 이름의 현금이 있습니다.");
			nameTextView.setText("");
			return true;
		}
		
		String money = MilkUtil.getCommaDel(moneyTextView.getText().toString());
		
		if (money.equals("") || money.length() < 1)
		{
			MilkUtil.showDialog(superContext, "확인", "금액을 입력해주세요.");
			return true;
		}
		
		return false;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == NAME_REQUEST_CODE)
		{
			String str = data.getStringExtra(NameActivity.NAME_DATA);
			nameTextView.setText(str);
		}
		else if (requestCode == MONEY_REQUEST_CODE)
		{
			String str = data.getStringExtra(MoneyActivity.MONEY_DATA);
			moneyTextView.setText(str);
		}
		else if (requestCode == COUNT_REQUEST_CODE)
		{
			String str = data.getStringExtra(CountActivity.COUNT_DATA);
			countTextView.setText(str);
		}
		else if (requestCode == MEMO_REQUEST_CODE)
		{
			String str = data.getStringExtra(MemoActivity.MEMO_DATA);
			memoTextView.setText(str);
		}
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(superContext, TipInfoActivity.class);
		intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_ASSET_REALMONEY_INFO);
		startActivity(intent);
	}

}
